const sip = require("sip");
const log4js = require('../../../utils/logUtils');
const logger = log4js.getLogger('sip');

const fxp = require("fast-xml-parser");
const aliveHandler = require("./aliveHandler");
const catalogHandler = require("./messages/catalogHandler");
const recordHandler = require("./messages/recordHandler");

const TAG = "[向上级连] [消息处理]";
/**
 * 收到 message 后的处理
 * @type {{handle: ((function(*, *): Promise<void>)|*)}}
 */
module.exports = {
    handle: async (rq) => {
        const content = fxp.parse(rq.content);

        logger.info(TAG, "==接收MESSAGE解析==:", JSON.stringify(content));
        if (content.Control) {
            sip.send(sip.makeResponse(rq, 200, 'OK'));
        } else if (content.Query.CmdType === 'Catalog') {
            await catalogHandler.handle(rq, content);
        } else if(content.Query.CmdType === 'RecordInfo'){
            await recordHandler.handle(rq, content);
        }
        aliveHandler.INTERVAL_CHANNEL(content);
    }
}
